#include "TransitionViewNode.h"
#include <ace/xcomponent/native_interface_xcomponent.h>
#include <arkui/native_animate.h>

static constexpr ArkUI_NodeEventType TRANSITION_VIEW_EVENT_TYPES[] = {NODE_ON_CLICK};

namespace rnoh {
TransitionViewNode::TransitionViewNode()
    : ArkUINode(NativeNodeApi::getInstance()->createNode(ArkUI_NodeType::ARKUI_NODE_STACK)) {
    for (auto eventType : TRANSITION_VIEW_EVENT_TYPES) {
        maybeThrow(NativeNodeApi::getInstance()->registerNodeEvent(m_nodeHandle, eventType, eventType, this));
    }
}

TransitionViewNode::~TransitionViewNode() {
    for (auto eventType : TRANSITION_VIEW_EVENT_TYPES) {
        NativeNodeApi::getInstance()->unregisterNodeEvent(m_nodeHandle, eventType);
    }
}

TransitionViewNode &TransitionViewNode::setTransitionViewDelegate(TransitionViewDelegate *TransitionViewDelegate) {
    m_TransitionViewDelegate = TransitionViewDelegate;
    return *this;
}

void TransitionViewNode::insertChild(ArkUINode &child, std::size_t index) {
    maybeThrow(NativeNodeApi::getInstance()->addChild(m_nodeHandle, child.getArkUINodeHandle()));
}

void TransitionViewNode::removeChild(ArkUINode &child) {
    maybeThrow(NativeNodeApi::getInstance()->removeChild(m_nodeHandle, child.getArkUINodeHandle()));
}

TransitionViewNode &TransitionViewNode::setLabel(const std::string &src) {
    ArkUI_AttributeItem labelItem = {.string = src.c_str()};

    maybeThrow(NativeNodeApi::getInstance()->setAttribute(m_nodeHandle, NODE_OPACITY_TRANSITION, &labelItem));
    return *this;
}

void TransitionViewNode::onNodeEvent(ArkUI_NodeEventType eventType, EventArgs &eventArgs) {
    if (eventType == ArkUI_NodeEventType::NODE_ON_CLICK) {
        if (m_TransitionViewDelegate != nullptr) {
            m_TransitionViewDelegate->onTransitionClick();
        }
        static ArkUI_NodeHandle handle = NativeNodeApi::getInstance()->createNode(ARKUI_NODE_STACK);
        LOG(INFO) << "TESTCS " << NativeNodeApi::getInstance()->getAttribute(handle, NODE_HEIGHT);
        ArkUI_NativeAnimateAPI_1 *animateApi = nullptr;
        OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_ANIMATE, ArkUI_NativeAnimateAPI_1, animateApi);
        ArkUI_AnimateOption *option = OH_ArkUI_AnimateOption_Create();
        OH_ArkUI_AnimateOption_SetDuration(option, 2000);
        OH_ArkUI_AnimateOption_SetTempo(option, 1.1);
        OH_ArkUI_AnimateOption_SetCurve(option, ARKUI_CURVE_EASE);
        OH_ArkUI_AnimateOption_SetIterations(option, 20);
        OH_ArkUI_AnimateOption_SetPlayMode(option, ARKUI_ANIMATION_PLAY_MODE_REVERSE);

        ArkUI_ExpectedFrameRateRange *range = new ArkUI_ExpectedFrameRateRange; // 帧率
        range->min = 10;
        range->max = 120;
        range->expected = 60;
        OH_ArkUI_AnimateOption_SetExpectedFrameRateRange(option, range);

        struct AA {
            int32_t a;
        };

        AA *onFinishUser = new AA;
        onFinishUser->a = 101;
        ArkUI_AnimateCompleteCallback *completeCallback = new ArkUI_AnimateCompleteCallback;
        completeCallback->userData = onFinishUser;
        completeCallback->type = ARKUI_FINISH_CALLBACK_REMOVED;
        completeCallback->callback = [](void *userData) {
            LOG(INFO) << "TESTCS completeCallback " << reinterpret_cast<AA *>(userData) -> a;
        };

        AA *eventUser = new AA;
        eventUser->a = 201;

        static bool isBack = true;
        ArkUI_ContextCallback *update = new ArkUI_ContextCallback;
        update->userData = eventUser;
        update->callback = [](void *user) {
            if (isBack) {
                ArkUI_NumberValue custom_widthValue[] = {200};
                ArkUI_AttributeItem custom_widthItem = {custom_widthValue, 1};
                ArkUI_NumberValue custom_heightVale[] = {80};
                ArkUI_AttributeItem custom_heightItem = {custom_heightVale, 1};
                NativeNodeApi::getInstance()->setAttribute(handle, NODE_WIDTH, &custom_widthItem);
                NativeNodeApi::getInstance()->setAttribute(handle, NODE_HEIGHT, &custom_heightItem);
            } else {
                ArkUI_NumberValue custom_widthValue[] = {100};
                ArkUI_AttributeItem custom_widthItem = {custom_widthValue, 1};
                ArkUI_NumberValue custom_heightVale[] = {40};
                ArkUI_AttributeItem custom_heightItem = {custom_heightVale, 1};
                NativeNodeApi::getInstance()->setAttribute(handle, NODE_WIDTH, &custom_widthItem);
                NativeNodeApi::getInstance()->setAttribute(handle, NODE_HEIGHT, &custom_heightItem);
            }
        };
        LOG(INFO) << "TESTCS context " << this << " " << OH_ArkUI_GetContextByNode(m_nodeHandle);
        
        animateApi->animateTo(OH_ArkUI_GetContextByNode(handle) , option, update, completeCallback);
//         OH_NativeXComponent_AttachNativeRootNode(component, m_nodeHandle);
    }
}
} // namespace rnoh